# -*- coding: utf-8 -*-
# @Time : 2021/12/23 17:14
# @Author : Limusen
# @File : demo_zhengze_01

import re
import jsonpath

# request_info = {'测试用例编号': 'api_cese_01', '测试用例名称': '获取用户信息', '用例执行': '是', '用例步骤': 'step_02', '接口名称': '获取用户信息',
#                 '请求方式': 'get',
#                 '请求地址': '/gateway/system/user/userInfo/oneself', '请求参数(get)': '', '请求参数(post)': '',
#                 '取值方式': 'jsonpath取值',
#                 '取值代码': '"userId":"(.+?)","nickName":"(.+?)"', '取值变量': 'userid,nickName',
#                 '断言类型': 'json_key_value', '期望结果': '{"code":"100001"}'}
request_info_json_path = {'测试用例编号': 'api_cese_01', '测试用例名称': '获取用户信息', '用例执行': '是', '用例步骤': 'step_02', '接口名称': '获取用户信息',
                          '请求方式': 'get',
                          '请求地址': '/gateway/system/user/userInfo/oneself', '请求参数(get)': '', '请求参数(post)': '',
                          '取值方式': 'jsonpath取值',
                          '取值代码': '$..userId', '取值变量': 'userid',
                          '断言类型': 'json_key_value', '期望结果': '{"code":"100001"}'}
request_info_1 = {'测试用例编号': 'api_cese_01', '测试用例名称': '获取用户信息', '用例执行': '是', '用例步骤': 'step_02', '接口名称': '获取用户信息',
                  '请求方式': 'get',
                  '请求地址': '/gateway/system/user/userInfo/oneself', '请求参数(get)': '', '请求参数(post)': '', '取值方式': '正则取值',
                  '取值代码': '"userId":"(.+?)"', '取值变量': 'userid',
                  '断言类型': 'json_key_value', '期望结果': '{"code":"100001"}'}

request_info_g = {'测试用例编号': 'api_cese_01', '测试用例名称': '获取用户信息', '用例执行': '是', '用例步骤': 'step_02', '接口名称': '获取用户信息',
                  '请求方式': 'get',
                  '请求地址': '/gateway/system/user/userInfo/oneself', '请求参数(get)': '', '请求参数(post)': '',
                  '取值方式': 'jsonpath取值',
                  '取值代码': '$.userId', '取值变量': 'userid',
                  '断言类型': 'json_key_value', '期望结果': '{"code":"100001"}'}

cai = '"data":"{"测试用例编号":"api_cese_01","请求参数(get)": "","userId":"测试奥什","phone":"17717870595","取值变量": "userid,nick_name","nickName":"恭喜发财"}"'
cai2 = '"data":"{"测试用例编号":"api_cese_01","请求参数(get)": "","userId":"张二狗","phone":"17717870595","取值变量": "userid,nick_name","nickName":"恭喜发财"}"'
json_0 = {
    'data': '{"测试用例编号":"api_cese_01","请求参数(get)": "","userId":"张二狗","phone":"17717870595","取值变量": "userid,nick_name","nickName":"恭喜发财"}'}

# #　示例代码
# num_list = [1, 2, 3, 4]
# name_list = ["one", "two", "three", "four"]
# print({name_list[i]: num_list[i] for i in range(len(num_list))})

# #　取多个值
# temp_variables = {}
# if request_info["取值方式"] == "正则取值":
#     value_list = request_info['取值代码'].split(',')
#     key_list = request_info['取值变量'].split(',')
#     for c in range(0, len(value_list)):
#         temp_variables[key_list[c]] = re.findall(value_list[c], cai)[0]
# print(temp_variables)

# # 取单个值
# temp_variables = {}
# if request_info_1["取值方式"] == "正则取值":
#     value_list = request_info_1['取值代码'].split(',')
#     key_list = request_info_1['取值变量'].split(',')
#     for c in range(0, len(value_list)):
#         temp_variables[key_list[c]] = re.findall(value_list[c], cai)[0]
# print(temp_variables)

# # # 多个取值
# json_path_list = {
#     "access_token": "52_obqRF3SEhpRjxopJ2thI8oc7VcsVnjUl3wXIi0HGL1p6Ry4wLb2jjrpM6lLk49O7Y8cn1JwkBAUbX1QPJIdgMDyu8mcWP1yeqp17WRAuNpn3CVlMIjPbGZhy9dh1gio461Z-8eb_cN4JqB59FMShABAIPV",
#     "expires_in": 7200,
#     "sads": 545,
#     "data": {"测试用例编号": "api_cese_01", "请求参数(get)": "", "userId": "张二狗", "phone": "17717870595",
#              "取值变量": "userid,nick_name", "nickName": "恭喜发财"}
# }
# value_list = {'取值方式': 'jsonpath取值', '取值代码': '$..userId,$..测试用例编号', '取值变量': 'userid,测试用例编号'}
# value = jsonpath.jsonpath(json_path_list, '$.expires_in')
#
# # 多个取值
# temp_variables = {}
# if value_list["取值方式"] == "jsonpath取值":
#     values = value_list['取值代码'].split(',')
#     keys = value_list['取值变量'].split(',')
#     for index in range(0, len(values)):
#         temp_variables[keys[index]] = jsonpath.jsonpath(json_path_list, values[index])[0]
# print(temp_variables)

# 整合一下
# temp_variables = {}
# if request_info["取值方式"] == "正则取值":
#     value_list = request_info['取值代码'].split(',')
#     key_list = request_info['取值变量'].split(',')
#     for index in range(0, len(value_list)):
#         temp_variables[key_list[index]] = re.findall(value_list[index], cai)[0]
# elif request_info["取值方式"] == "jsonpath取值":
#     values = request_info['取值代码'].split(',')
#     keys = request_info['取值变量'].split(',')
#     for index in range(0, len(values)):
#         temp_variables[keys[index]] = jsonpath.jsonpath(json_path_list, values[index])[0]
# print(temp_variables)

result_list = '{"code": "100001","desc": "成功","success": "true","data": "{"token": "I0k",  "activation":"true"}"}'
result_json = {"code": "100001", "desc": "成功", "success": "true", "data": {"token": "I0k", "activation": "true"}}

request_info = {'测试用例编号': 'api_cese_01', '测试用例名称': '获取用户信息', '用例执行': '是', '用例步骤': 'step_02', '接口名称': '获取用户信息',
                '请求方式': 'get',
                '请求地址': '/gateway/system/user/userInfo/oneself', '请求参数(get)': '', '请求参数(post)': '',
                '取值方式': 'jsonpath取值',
                '取值代码': '$.code,$..token', '取值变量': 'code,token',
                '断言类型': 'json_key_value', '期望结果': '{"code":"100001"}'}
temp_variables = {}
if request_info["取值方式"] == "正则取值":
    value_list = request_info['取值代码'].split(',')
    key_list = request_info['取值变量'].split(',')
    for index in range(0, len(value_list)):
        # 如果这个地方报错了，请查看正则表达式或者jsonpath取值模板是否有问题
        # 这里需要注意的是 取出字符串数据,中间是没有空格的
        # 例如"code": "100001"  正则模板应该为 "code":"(.+?)"
        temp_variables[key_list[index]] = re.findall(value_list[index], result_list)[0]
elif request_info["取值方式"] == "jsonpath取值":
    value_list = request_info['取值代码'].split(',')
    key_list = request_info['取值变量'].split(',')
    for index in range(0, len(value_list)):
        temp_variables[key_list[index]] = jsonpath.jsonpath(result_json, value_list[index])[0]
print(temp_variables)
